//
//  PresentOneTransition.m
//  BriefBookAnimation
//
//  Created by Burn on 2019/6/27.
//  Copyright © 2019 menksoft. All rights reserved.
//

#import "PresentOneTransition.h"

@interface PresentOneTransition ()

@property(assign,nonatomic) PresentOneTransitionType type;


@end


@implementation PresentOneTransition

- (instancetype)initWith:(PresentOneTransitionType)type{
    if (self = [super init]) {
        self.type = type;
    }
    return self;
}
+ (instancetype)initWith:(PresentOneTransitionType)type{
    return [[self alloc]initWith:type];
}


- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return .7;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    switch (self.type) {
        case PresentOneTransitionTypePresent:
            [self presentAnimationWith:transitionContext];
            break;
        case PresentOneTransitionTypeDismiss:
            [self dismissAnimationWith:transitionContext];
            break;
            
        default:
            break;
    }
}

- (void)presentAnimationWith:(id<UIViewControllerContextTransitioning>)context{
    UIViewController *toVC = [context viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController *fromeVC = [context viewControllerForKey:UITransitionContextFromViewControllerKey];
    //  snapshotViewAfterScreenUpdates可以对某个视图截图，我们采用对这个截图做动画代替直接对AVC做动画，因为在手势过渡中直接使用AVC动画会和手势有冲突， 如果不需要实现手势的话，就可以不是用截图视图了，大家可以自行尝试一下,也就是动画执行完[context completeTransition:YES];时候控制器会消失
    UIView *tempView = [fromeVC.view snapshotViewAfterScreenUpdates:NO];
    tempView.frame = fromeVC.view.frame;
    //  因为对截图做动画，AVC就可以隐藏了
    fromeVC.view.hidden = YES;
    //  这里有个重要的概念containerView，如果要对视图做转场动画，视图就必须要加入containerView中才能进行，可以理解containerView管理着所有做转场动画的视图
    

    
    UIView *contianerView = [context containerView];
    //  将截图视图添加
    [contianerView addSubview:tempView];
    [contianerView addSubview:toVC.view];
    //  设置BVC的frame,因为这里BVCpresent出来不是全屏,且初始的时候字底部,如果不设置frame的话默认就是整个屏幕,这里containerView的frame就是整个屏幕
    toVC.view.frame = CGRectMake(0, contianerView.frame.size.height, contianerView.frame.size.width, 400);
    //  开始动画吧,使用弹簧效果的动画API
    [UIView animateWithDuration:[self transitionDuration:context] delay:0 usingSpringWithDamping:.55 initialSpringVelocity:.5 options:0 animations:^{
        //  享受我们让BVC向上移动
        toVC.view.transform = CGAffineTransformMakeTranslation(0, -400);
        //  然后让截图视图缩小-一点即可
        tempView.transform = CGAffineTransformMakeScale(.9, .9);
        
        tempView.layer.cornerRadius = 10;
        tempView.layer.masksToBounds = YES;
        
        toVC.view.layer.cornerRadius = 10;
        toVC.view.layer.masksToBounds = YES;
        
    } completion:^(BOOL finished) {
        //  使用如下代码标记整个转场过程是否正常完成[transitionContext transitionWasCancelled]代表手势是否取消了,如果取消了就传NO表示转场失败,如果不用手势present的话直接传YES也是可以的,但是l无论如何我们都必须标记c转场的状态
        [context completeTransition:![context transitionWasCancelled]];
        if ([context transitionWasCancelled]) {
            //  失败后,我们把AVC显示出来
            fromeVC.view.hidden = NO;
            [tempView removeFromSuperview];
            
            toVC.view.layer.cornerRadius = 0;
            toVC.view.layer.masksToBounds = NO;
        }
    }];
}
- (void)dismissAnimationWith:(id<UIViewControllerContextTransitioning>)context{
    //  注意dismiss的时候fromVC就是BVC了,toVC也就是AVC了
    UIViewController *toVC = [context viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController *fromeVC = [context viewControllerForKey:UITransitionContextFromViewControllerKey];
    //  参照present动画的逻辑,present成功后,containerView的追后一个视图就是截图视图,我们将其取出准备动画
    UIView *tempView = [[context containerView] subviews][0];   //  也就是AViewController
    //动画
    [UIView animateWithDuration:.25 animations:^{
        fromeVC.view.transform = CGAffineTransformIdentity;
        tempView.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
            //  失败了标记失败
        if ([context transitionWasCancelled]) {
            [context completeTransition:NO];
        }else{
            //  成功了,我们需要标记成功,同时让vc1显示出来,然后移除截图视图
            [context completeTransition:YES];
            toVC.view.hidden = NO;
            [tempView removeFromSuperview];
        }
        
    }];
}



@end
